<?php

namespace app\controllers;

use app\models\Admins;
use lithium\security\Auth;
use lithium\action\DispatchException;

class AdminsController extends \lithium\action\Controller {

	public function login() {
		if(!empty($this->request->data)){
			if(Auth::check('default', $this->request)){
				return $this->redirect('/');
			}
		}
	}

	public function logout() {
		Auth::clear('default');
        return $this->redirect('/');
	}

	public function index() {

		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}

		$admins = Admins::all();
		return compact('admins');
	}

	public function view() {

		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}
		
		$admin = Admins::find($this->request->id);
		return compact('admin');
	}

	public function add() {

		if (!Auth::check('default')) {
			return $this->redirect('Sessions::add');
		}

		$admin = Admins::create();

		if (($this->request->data) && $admin->save($this->request->data)) {
			return $this->redirect(array('Admins::view', 'args' => array($admin->id)));
		}
		return compact('admin');
	}

	public function edit() {
		$admin = Admins::find($this->request->id);
		if (!$admin) {
			return $this->redirect('Admins::index');
		}
		if (($this->request->data) && $admin->save($this->request->data)) {
			return $this->redirect(array('Admins::view', 'args' => array($admin->id)));
		}
		return compact('admin');
	}

	public function delete() {
		if (!$this->request->is('post') && !$this->request->is('delete')) {
			$msg = "Admins::delete can only be called with http:post or http:delete.";
			throw new DispatchException($msg);
		}
		Admins::find($this->request->id)->delete();
		return $this->redirect('Admins::index');
	}
}

?>